home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Informant Complete 1995 - 2000
/
Delphi Informant Complete 1995 to 2000.iso
/
Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar
/
1998
/
Apr
/
di9804rs
/
AAlias2.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-12-29
|
8KB
|
267 lines
unit AAlias2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TAntiAliasForm = class(TForm)
OutBox: TPaintBox;
OrigBox: TPaintBox;
Label1: TLabel;
Label2: TLabel;
BigBox: TPaintBox;
Label3: TLabel;
function RGB(r, g, b : Integer) : TColor;
procedure SeparateColor(color : TColor; var r, g, b : Integer);
procedure AAliasPicture;
procedure SetPalette(bm : TBitmap);
procedure FormCreate(Sender: TObject);
procedure BigBoxPaint(Sender: TObject);
procedure OutBoxPaint(Sender: TObject);
procedure DrawFace(bm : TBitmap; pen_width : Integer);
procedure OrigBoxPaint(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
AntiAliasForm: TAntiAliasForm;
implementation
{$R *.DFM}
var
orig_bm, big_bm, out_bm : TBitmap;
function TAntiAliasForm.RGB(r, g, b : Integer) : TColor;
begin
Result := r + 256 * (g + 256 * b);
end;
procedure TAntiAliasForm.SeparateColor(color : TColor;
var r, g, b : Integer);
begin
r := color Mod 256;
g := (color Div 256) Mod 256;
b := color Div 65536;
end;
procedure TAntiAliasForm.AAliasPicture;
var
x, y, i, j : Integer;
r, g, b, totr, totg, totb : Integer;
begin
// Display the hourglass cursor.
Screen.Cursor := crHourGlass;
// The "- 3" keeps us from falling off the edge
// of BigBox. Over the edge the Pixel value returns
// -1 and messes up the colors.
for y := 0 to (big_bm.Height - 3) Div 2 do
begin
for x := 0 to (big_bm.Width - 3) Div 2 do
begin
// Compute the value of output pixel (x, y).
totr := 0;
totg := 0;
totb := 0;
for j := 0 to 1 do
begin
for i := 0 to 1 do
begin
SeparateColor(big_bm.Canvas.Pixels
[2 * x + i, 2 * y + j], r, g, b);
totr := totr + r;
totg := totg + g;
totb := totb + b;
end;
end;
out_bm.Canvas.Pixels[x, y] :=
RGB(totr Div 4, totg Div 4, totb Div 4);
end;
end;
OutBox.Invalidate;
// Remove the hourglass cursor.
Screen.Cursor := crDefault;
end;
// Create a color palette including various combinations
// of yellow, white, black, and aqua.
procedure TAntiAliasForm.SetPalette(bm : TBitmap);
var
r, g, b : array [1..4] of Integer;
totr, totg, totb : Integer;
clr, i1, i2, i3, i4 : Integer;
pal : PLogPalette;
hpal : HPALETTE;
begin
pal := nil;
try
GetMem(pal, sizeof(TLogPalette) + sizeof(TPaletteEntry) * 255);
pal.palVersion := $300;
// Calculate RGB values for the colors.
SeparateColor(clYellow, r[1], g[1], b[1]);
SeparateColor(clWhite, r[2], g[2], b[2]);
SeparateColor(clBlack, r[3], g[3], b[3]);
SeparateColor(clAqua, r[4], g[4], b[4]);
// Calculate all combinations of the colors
clr := 0;
for i1 := 0 to 4 do
begin
for i2 := 0 to 4 - i1 do
begin
for i3 := 0 to 4 - i1 - i2 do
begin
// Create the color entry.
i4 := 4 - i1 - i2 - i3;
totr := i1 * r[1] + i2 * r[2] +
i3 * r[3] + i4 * r[4];
totg := i1 * g[1] + i2 * g[2] +
i3 * g[3] + i4 * g[4];
totb := i1 * b[1] + i2 * b[2] +
i3 * b[3] + i4 * b[4];
pal.palPalEntry[clr].peRed := Byte(Round(totr / 4));
pal.palPalEntry[clr].peGreen := Byte(Round(totg / 4));
pal.palPalEntry[clr].peBlue := Byte(Round(totb / 4));
clr := clr + 1;
end;
end;
end;
pal.palNumEntries := clr;
hpal := CreatePalette(pal^);
if hpal <> 0 then bm.Palette := hpal;
finally
FreeMem(pal);
end;
end;
procedure TAntiAliasForm.FormCreate(Sender: TObject);
begin
// Create the necessary bitmaps.
orig_bm := TBitmap.Create;
orig_bm.Width := OrigBox.ClientWidth;
orig_bm.Height := OrigBox.ClientHeight;
big_bm := TBitmap.Create;
big_bm.Width := 2 * orig_bm.Width;
big_bm.Height := 2 * orig_bm.Height;
out_bm := TBitmap.Create;
out_bm.Width := orig_bm.Width;
out_bm.Height := orig_bm.Height;
// Draw the original picture.
DrawFace(orig_bm, 2); {Changed by RLV on 12/29/97}
// Draw the enlarged picture.
DrawFace(big_bm, 3); {Changed by RLV on 12/29/97}
// Give the final picture a good color palette.
SetPalette(out_bm);
// Create the anti-aliased version.
AAliasPicture;
end;
procedure TAntiAliasForm.BigBoxPaint(Sender: TObject);
begin
BigBox.Canvas.Draw(0, 0, big_bm);
end;
procedure TAntiAliasForm.OutBoxPaint(Sender: TObject);
begin
OutBox.Canvas.Draw(0, 0, out_bm);
end;
// Draw a smiley face for OrigBox.
procedure TAntiAliasForm.DrawFace(bm : TBitmap;
pen_width : Integer);
var
x1, y1, x2, y2, x3, y3, x4, y4 : Integer;
old_width : Integer;
old_color : TColor;
begin
// Save the original brush color and pen width.
old_width := bm.Canvas.Pen.Width;
old_color := bm.Canvas.Brush.Color;
// Draw the head.
bm.Canvas.Pen.Width := pen_width;
bm.Canvas.Brush.Color := clYellow;
x1 := Round(bm.Width * 0.05);
y1 := x1;
x2 := Round(bm.Height * 0.95);
y2 := x2;
bm.Canvas.Ellipse(x1, y1, x2, y2);
// Draw the eyes.
bm.Canvas.Brush.Color := clWhite;
x1 := Round(bm.Width * 0.25);
y1 := Round(bm.Height * 0.25);
x2 := Round(bm.Width * 0.4);
y2 := Round(bm.Height * 0.4);
bm.Canvas.Ellipse(x1, y1, x2, y2);
x1 := Round(bm.Width * 0.75);
x2 := Round(bm.Width * 0.6);
bm.Canvas.Ellipse(x1, y1, x2, y2);
// Draw the pupils.
bm.Canvas.Brush.Color := clBlack;
bm.Canvas.Refresh;
x1 := Round(bm.Width * 0.275);
y1 := Round(bm.Height * 0.3);
x2 := Round(bm.Width * 0.375);
y2 := Round(bm.Height * 0.4);
bm.Canvas.Ellipse(x1, y1, x2, y2);
x1 := Round(bm.Width * 0.725);
x2 := Round(bm.Width * 0.625);
bm.Canvas.Ellipse(x1, y1, x2, y2);
// Draw the nose.
bm.Canvas.Brush.Color := clAqua;
x1 := Round(bm.Width * 0.425);
y1 := Round(bm.Height * 0.425);
x2 := Round(bm.Width * 0.575);
y2 := Round(bm.Height * 0.6);
bm.Canvas.Ellipse(x1, y1, x2, y2);
// Draw a crooked smile.
x1 := Round(bm.Width * 0.25);
y1 := Round(bm.Height * 0.25);
x2 := Round(bm.Width * 0.75);
y2 := Round(bm.Height * 0.75);
x3 := Round(bm.Width * 0.4);
y3 := Round(bm.Height * 0.6);
x4 := Round(bm.Width * 0.8);
y4 := Round(bm.Height * 0.6);
bm.Canvas.Arc(x1, y1, x2, y2, x3, y3, x4, y4);
bm.Canvas.Brush.Color := old_color;
end;
procedure TAntiAliasForm.OrigBoxPaint(Sender: TObject);
begin
OrigBox.Canvas.Draw(0, 0, orig_bm);
end;
procedure TAntiAliasForm.FormDestroy(Sender: TObject);
begin
orig_bm.Free; {Added by RLV on 12/29/97}
big_bm.Free; {Added by RLV on 12/29/97}
out_bm.Free; {Added by RLV on 12/29/97}
end;
end.